package com.zjzywl.smoke.controller;

import java.util.List;

import javax.websocket.server.PathParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjzywl.smoke.authenticate.AuthUser;
import com.zjzywl.smoke.repository.OrganizationRepository;
import com.zjzywl.smoke.repository.SysUserRepository;
import com.zjzywl.smoke.util.RestResult;
import com.zjzywl.smoke.util.WebResult;
import com.zjzywl.smoke.entity.Organization;
import com.zjzywl.smoke.entity.SysUser;

@RestController
@RequestMapping(value = "/organization")
public class OrganizationController extends BaseController {
	@Autowired
	private OrganizationRepository repository;

	@Autowired
	private SysUserRepository userRepository;

	// 获取组织列表
	@RequestMapping(value = "/list")
	public List<Organization> getOrganizations(Model model) {
		List<Organization> orgs = repository.findOrderLevel();
		return orgs;
	}

	// 获取单个组织
	@RequestMapping(value = "/edit/id/{id}")
	public Organization editOrganizationById(@PathVariable Integer id) {
		Organization org = repository.findById(id);
		return org;
	}

	// 获取单个组织下的所有组织
	@RequestMapping(value = "/getByPath/{id}")
	public List<Organization> editOrganizationAllById(@PathVariable("id") Integer id) {
		Organization org = repository.findById(id);
		List<Organization> orgs = repository.findAllByPath(org.getPath() + "%");
		return orgs;
	}

	// 获取组织列表
	@RequestMapping(value = "/data/list")
	public List<Organization> getOrganizationData(@PathParam("loginName") String loginName,
			@PathParam("type") String type) {
		System.out.println(loginName);
		// 获取用户信息
		SysUser user = userRepository.findByLoginName(loginName);
		System.out.println(type);
		List<Organization> orgs = null;
		if (type.equals("null")) {
			orgs = repository.findByPathAll(user.getOrg().getPath() + "%");
		} else {
			orgs = repository.findByPath(user.getOrg().getPath() + "%", type);
		}
		return orgs;
	}

	// 根据组织编号获取组织
	@RequestMapping(value = "/{Orgid}")
	public Organization getOrganization(@PathVariable("Orgid") String Orgid) {
		return repository.findByOrgid(Orgid);
	}

	
	
	// 保存组织信息
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public ResponseEntity<Organization> save(@RequestBody Organization org, @PathParam("parentid") Integer parentid) {
		try {
			Organization parent = repository.findById(parentid);
			if (parent != null) {
				org.setParent(parent);
				org.setLevel(getLevel(parent));
				org.setPath(parent.getPath() + "|" + org.getOrgid());
			} else {
				org.setLevel(0);
				org.setPath(org.getOrgid());
			}
			Organization newOrg = repository.save(org);

			return RestResult.OK(newOrg);

		} catch (Exception e) {
			return RestResult.notFound(e.getMessage());
		}
	}

	// 删除组织信息
	@RequestMapping(value = "/delete/{id}")
	public WebResult delete(@PathVariable("id") Integer id) {
		try {
			Organization org = repository.findOne(id);
			org.setRemove("删除");
			repository.save(org);
			return WebResult.OK();
		} catch (Exception e) {
			return WebResult.FAIL(e.getMessage());
		}

	}

	// 判断组织层级
	private Integer getLevel(Organization parent) {
		try {
			if (parent.getParent() == null) {
				return 1;
			}
			if (parent.getParent().getParent() == null) {
				return 2;
			}
			if (parent.getParent().getParent().getParent() == null) {
				return 3;
			}
			if (parent.getParent().getParent().getParent().getParent() == null) {
				return 4;
			}
			return 5;
		} catch (Exception e) {
			// TODO: handle exception
			return 0;
		}

	}
}
